﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Windows.Forms;

namespace game
{
    public partial class UserControlpintu : UserControl
    {
        public UserControlpintu()
        {
            InitializeComponent();

            this.Disposed += UserControlpintu_Disposed;
        }

        private void UserControlpintu_Disposed(object sender, EventArgs e)
        {
            //throw new NotImplementedException();
            clearresouce(this);
        }

        private void clearresouce(Control cc)
        {
            foreach (Control item in cc.Controls)
            {
                clearresouce(item);
            }

            if (cc is PictureBox)
            {
                (cc as PictureBox).Image.Dispose();
            }
        }

        public UserControlpintu(string img_fullname)
        {
            // TODO: Complete member initialization

            //string img_fullname = @"D:\all\常用资料\photo\2016\IMG_1944.JPG";

            DirectoryInfo d1 = new DirectoryInfo(Path.Combine(Application.StartupPath, "pintu"));
            if (!d1.Exists) { d1.Create(); }

            Bitmap bb = new System.Drawing.Bitmap(img_fullname);

            var sx = 0;
            var sy = 0;
            var x = bb.Width / 3;
            var y = bb.Height / 3;
            var count = 1;

            for (var i = 0; i < 3; i++)
            {
                for (var j = 0; j < 3; j++)
                {
                    var therectangle = new Rectangle(sx, sy, x, y);
                    using (Bitmap clonebitmap = bb.Clone(therectangle, PixelFormat.DontCare))
                    {
                        clonebitmap.Save(Path.Combine(Application.StartupPath, "pintu//" + count + ".png"), ImageFormat.Png);
                    }

                    count++;
                    sx = sx + x;
                }
                sx = 0;
                sy = sy + y;
            }

            InitializeComponent();

            this.Disposed += UserControlpintu_Disposed;
        }

        private void UserControlpintu_Load(object sender, EventArgs e)
        {
            jiazaitupian();

            tuozhuaixiaoguo();

            yanzheng();
        }

        private System.Windows.Forms.Timer t = new Timer();

        private void yanzheng()
        {
            //throw new NotImplementedException();

            t.Interval = 100;
            t.Tick += t_Tick;
            t.Start();
        }

        private void t_Tick(object sender, EventArgs e)
        {
            //throw new NotImplementedException();
            foreach (var item in this.groupBox1.Controls)
            {
                if (((item as PictureBox).Tag as ArrayList)[0].ToString() != ((item as PictureBox).Tag as ArrayList)[1].ToString())
                {
                    return;
                }
            }
            this.pictureBox10.Visible = true;
            //t.Stop();
        }

        private Point mouseDownPoint;
        private bool isMove;

        private void tuozhuaixiaoguo()
        {
            int ox = 0;
            int oy = 0;

            //throw new NotImplementedException();
            for (var i = 1; i < 10; i++)
            {
                (this.groupBox1.Controls[i - 1] as PictureBox).MouseDown += delegate (object sender, MouseEventArgs e)
                {
                    if (e.Button == MouseButtons.Left)
                    {
                        mouseDownPoint.X = Cursor.Position.X;
                        mouseDownPoint.Y = Cursor.Position.Y;
                        ox = (sender as PictureBox).Location.X;
                        oy = (sender as PictureBox).Location.Y;

                        isMove = true;
                        //(this.groupBox1.Controls[i - 1] as PictureBox).Focus();
                        (sender as PictureBox).BringToFront();
                    }
                };

                (this.groupBox1.Controls[i - 1] as PictureBox).MouseMove += delegate (object sender, MouseEventArgs e)
                {
                    //(this.groupBox1.Controls[i - 1] as PictureBox).Focus();
                    if (isMove)
                    {
                        int x, y;
                        int moveX, moveY;
                        moveX = Cursor.Position.X - mouseDownPoint.X;
                        moveY = Cursor.Position.Y - mouseDownPoint.Y;
                        x = (sender as PictureBox).Location.X + moveX;
                        y = (sender as PictureBox).Location.Y + moveY;
                        (sender as PictureBox).Location = new Point(x, y);
                        mouseDownPoint.X = Cursor.Position.X;
                        mouseDownPoint.Y = Cursor.Position.Y;
                    }
                };

                (this.groupBox1.Controls[i - 1] as PictureBox).MouseUp += delegate (object sender, MouseEventArgs e)
                {
                    if (e.Button == MouseButtons.Left)
                    {
                        isMove = false;
                    }

                    //for (var m = 0; m < 9; m++)
                    //{
                    foreach (var item in this.groupBox1.Controls)
                    {
                        //var px = (this.groupBox1.Controls[m] as PictureBox);
                        if (item != sender)
                        {
                            var px = item as PictureBox;
                            if (
                              (sender as PictureBox).Location.X >= px.Location.X
                               &&
                              (sender as PictureBox).Location.X <= px.Location.X + width
                               &&
                              (sender as PictureBox).Location.Y >= px.Location.Y
                               &&
                              (sender as PictureBox).Location.Y <= px.Location.Y + height
                               )
                            {
                                (sender as PictureBox).Left = px.Location.X;
                                (sender as PictureBox).Top = px.Location.Y;
                                px.Left = ox;
                                px.Top = oy;

                                var temp = ((sender as PictureBox).Tag as ArrayList)[0];
                                ((sender as PictureBox).Tag as ArrayList)[0] = (px.Tag as ArrayList)[0];
                                (px.Tag as ArrayList)[0] = temp;

                                return;
                            }
                        }
                    }
                    (sender as PictureBox).Left = ox;
                    (sender as PictureBox).Top = oy;

                    //(this.groupBox1.Controls[i - 1] as PictureBox).Image = new Bitmap(Path.Combine(path, i + ".png"));
                    //}
                };

                //(this.groupBox1.Controls[i - 1] as PictureBox).MouseDown += new System.Windows.Forms.MouseEventHandler(this.picturebox_mouseenter);
                ////(this.groupBox1.Controls[i - 1] as PictureBox).MouseMove += new System.Windows.Forms.MouseEventHandler(this.picturebox_mousemove);
                //this.groupBox1.MouseMove += new MouseEventHandler(groupBox_MouseMove);

                //(this.groupBox1.Controls[i - 1] as PictureBox).AllowDrop = true;
                //(this.groupBox1.Controls[i - 1] as PictureBox).DragEnter += delegate(object sender, DragEventArgs e)
                //{
                //    e.Effect = DragDropEffects.Move;
                //};

                //(this.groupBox1.Controls[i - 1] as PictureBox).DragDrop += delegate(object sender, DragEventArgs e)
                //{
                //    //((Control)e.Data.GetData(typeof(PictureBox))) // 这里是传过来的控件
                //    ////下面写添加代码

                //};

                //(this.groupBox1.Controls[i - 1] as PictureBox).MouseMove += delegate(object sender, MouseEventArgs e)
                //{
                //    PictureBox lb = (PictureBox)sender;
                //    lb.DoDragDrop(lb, DragDropEffects.Move);
                //    //(sender as PictureBox).Location = new System.Drawing.Point(e.X, e.Y);
                //};
            }
        }

        private void groupBox_MouseMove(object sender, MouseEventArgs e)
        {
            //if (flag)
            //{
            //    if (c != null) { c.Location = new System.Drawing.Point(e.X, e.Y); }
            //}
        }

        private void picturebox_mousemove(object sender, MouseEventArgs e)
        {
            //if (c != null) { c.Location = new System.Drawing.Point(e.X, e.Y); }
        }

        private Control c;
        //private bool flag;

        private void picturebox_mouseenter(object sender, EventArgs e)
        {
            //flag = true;
            ////throw new NotImplementedException();
            c = sender as Control;

            c.BringToFront();
            c.AllowDrop = true;
        }

        private int width = 100;
        private int height = 120;

        private void jiazaitupian()
        {
            this.groupBox1.Width = 600;
            this.groupBox1.Height = 600;
            this.groupBox1.Paint += groupBox1_Paint;

            this.pictureBox10.Left = 700;
            this.pictureBox10.Visible = false;

            string path = Application.StartupPath + "\\pintu\\";
            //string[] tmp = new string[] { "1", "2", "3", "4", "5", "6", "7", "8", "9" };
            List<int> tmp = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
            var arr = new ArrayList();

            //调整距离
            int oldleft = 16;
            int oldtop = 20;

            //int oldleft = (this.groupBox1.Controls[0] as PictureBox).Left;
            //int oldtop = (this.groupBox1.Controls[0] as PictureBox).Top;

            for (var i = 0; i < 3; i++)
            {
                for (var j = 0; j < 3; j++)
                {
                    (this.groupBox1.Controls[i * 3 + j] as PictureBox).SizeMode = PictureBoxSizeMode.StretchImage;
                    (this.groupBox1.Controls[i * 3 + j] as PictureBox).Width = width;
                    (this.groupBox1.Controls[i * 3 + j] as PictureBox).Height = height;

                    (this.groupBox1.Controls[i * 3 + j] as PictureBox).Left = oldleft;
                    (this.groupBox1.Controls[i * 3 + j] as PictureBox).Top = oldtop;
                    //oldleft = (this.groupBox1.Controls[i * 3 + j] as PictureBox).ClientRectangle.Right;
                    oldleft = oldleft + width;
                }
                //oldtop = (this.groupBox1.Controls[i * 3] as PictureBox).ClientRectangle.Bottom;
                oldleft = 16;
                oldtop = oldtop + height;
            }

            for (var i = 1; i < 10; i++)
            {
                Random r = new Random();
                var d = r.Next(tmp.Count); //随机数
                var rid = tmp[d];
                tmp.Remove(tmp[d]);

                (this.groupBox1.Controls[i - 1] as PictureBox).Image = new Bitmap(Path.Combine(path, rid + ".png"));

                //精灵需要两个属性
                ArrayList a = new ArrayList();
                // 1个当前位置
                a.Add(i);
                //1个应该在的位置
                a.Add(rid);

                (this.groupBox1.Controls[i - 1] as PictureBox).Tag = a;
            }

            //int width = 167;
            //int height = 167;

            //PictureBox p1 = new PictureBox();
            //p1.Width = width;
            //p1.Height = height;

            //////string path = Application.StartupPath + "\\img\\";
            //string imgpath = Path.Combine(path, "1.png");
            //Image b1 = new Bitmap(imgpath);
            //p1.Image = b1;
            ////p1.Dock = DockStyle.Fill;

            //this.Controls.Add(p1);
            //pictureBox1.Image = b1;
            //pictureBox1.SizeMode = PictureBoxSizeMode.Normal;

            //var num = 1;

            //var rid;

            //if (path == "") { path = "first" }
            //图片宽高的动态调节
            //for (var i = 1; i < 4; i++)
            //{
            //    for (var j = 1; j < 4; j++)
            //    {
            //        //Rum();

            //        Random r = new Random();
            //        var d = r.Next(tmp.Length); //随机数
            //        var rid = tmp[d];

            //       this.groupBox1.Controls[]

            //        num++;
            //    }

            //}
        }

        private void groupBox1_Paint(object sender, PaintEventArgs e)
        {
            //e.Graphics.Clear(groupBox1.BackColor);
            //e.Graphics.DrawString(groupBox1.Text, groupBox1.Font, Brushes.Red, 10, 1);
            //e.Graphics.DrawRectangle(new Pen(Color.Black, 5), (sender as GroupBox).ClientRectangle);
            //base.OnPaint();

            //e.Graphics.DrawLine(Pens.Red, 1, 7, 8, 7);
            //e.Graphics.DrawLine(Pens.Red, e.Graphics.MeasureString(groupBox1.Text, groupBox1.Font).Width + 8, 7, groupBox1.Width - 2, 7);
            //e.Graphics.DrawLine(Pens.Red, 1, 7, 1, groupBox1.Height - 2);
            //e.Graphics.DrawLine(Pens.Red, 1, groupBox1.Height - 2, groupBox1.Width - 2, groupBox1.Height - 2);
            //e.Graphics.DrawLine(Pens.Red, groupBox1.Width - 2, 7, groupBox1.Width - 2, groupBox1.Height - 2);
        }

        private void UserControlpintu_MouseMove(object sender, MouseEventArgs e)
        {
        }

        private void groupBox1_MouseCaptureChanged(object sender, EventArgs e)
        {
        }

        private void button1_Click(object sender, EventArgs e)
        {
            jiazaitupian();
        }

        //private void Rum()
        //{
        //    //throw new NotImplementedException();
        //    Random r = new Random();
        //    var d = Math.Floor(r.Next(1,j)  * tmp.Length); //随机数
        //}
    }
}